﻿using Kingdee.BOS;
using Kingdee.BOS.App.Data;
using Kingdee.BOS.Contracts.Report;
using Kingdee.BOS.Core.Report;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DzInfo.Report.ND
{
    [Description("ND：驻点成本分析表_简单账表"), HotUpdate]
    public class ZhuDianReport : SysReportBaseService
    {
        public override void Initialize()
        {
            base.Initialize();
            this.ReportProperty.ReportType = ReportType.REPORTTYPE_NORMAL;
            this.ReportProperty.ReportName = new LocaleValue("驻点成本分析表", base.Context.UserLocale.LCID);
            this.IsCreateTempTableByPlugin = true;
            this.ReportProperty.IsUIDesignerColumns = false;
            this.ReportProperty.IsGroupSummary = true;
            this.ReportProperty.SimpleAllCols = false;
            this.ReportProperty.PrimaryKeyFieldName = "FKH";
            this.ReportProperty.IdentityFieldName = "FIDENTITYID";
        }
        public override ReportTitles GetReportTitles(IRptParams filter)
        {
            //F_OrgText F_StartDate F_EndDate
            ReportTitles titles = new ReportTitles();
            DynamicObject customFil = filter.FilterParameter.CustomFilter;
            DateTime startDate = DateTime.Now;
            DateTime endDate = DateTime.Now;
            string orgText = "";
            List<string> orgStr = new List<string>();
            if (customFil != null)
            {
                startDate = Convert.ToDateTime(customFil["F_POSW_StartDate"]);
                endDate = Convert.ToDateTime(customFil["F_POSW_EndDate"]);
                var orgList = customFil["F_POSW_MulOrgId"] as DynamicObjectCollection;
                foreach (var org in orgList)
                {
                    DynamicObject o = org["F_POSW_MulOrgId"] as DynamicObject;
                    string orgName = o["name"].ToString();
                    orgStr.Add(orgName);
                }
            }
            orgText = string.Join(",", orgStr);
            titles.AddTitle("F_StartDate", startDate.ToString("yyyy-MM-dd"));
            titles.AddTitle("F_OrgText", orgText);
            titles.AddTitle("F_EndDate", endDate.ToString("yyyy-MM-dd"));
            return titles;
        }
        protected override string BuilderTempTableOrderBySQL(IRptParams filter)
        {
            string sortStr = filter.FilterParameter.SortString;
            if (sortStr.Length > 0)
            {
                return sortStr;
            }
            return " FKH ASC,FEL ASC ";
        }
        public override void BuilderReportSqlAndTempTable(IRptParams filter, string tableName)
        {
            string seqFld = string.Format(base.KSQL_SEQ, BuilderTempTableOrderBySQL(filter));
            string createSql = GetCreateSql();

            List<string> ids = new List<string>();
            var customFilter = filter.FilterParameter.CustomFilter;
            var strDate = Convert.ToDateTime(customFilter["F_POSW_StartDate"]);
            var endDate = Convert.ToDateTime(customFilter["F_POSW_EndDate"]);
            var orgList = customFilter["F_POSW_MulOrgId"] as DynamicObjectCollection;
            foreach (var org in orgList)
            {
                string id = Convert.ToString(org["F_POSW_MulOrgId_Id"]);
                ids.Add(id);
            }
            string idstr = string.Join(",", ids);

            List<string> zdqyList = new List<string>();
            var zdqys = customFilter["FZDQY"] as DynamicObjectCollection;
            foreach (var zdqy in zdqys)
            {
                string id = Convert.ToString(zdqy["FZDQY_Id"]);
                zdqyList.Add(id);
            }
            string zdqyStr = string.Join(",", zdqyList);

            List<string> khList = new List<string>();
            var khs = customFilter["FKH"] as DynamicObjectCollection;
            foreach (var kh in khs)
            {
                string id = Convert.ToString(kh["FKH_Id"]);
                khList.Add(id);
            }
            string khStr = string.Join(",", khList);

            List<string> qxList = new List<string>();
            var qxs = customFilter["FQX"] as DynamicObjectCollection;
            foreach (var qx in qxs)
            {
                string id = Convert.ToString(qx["FQX_Id"]);
                qxList.Add(id);
            }
            string qxStr = string.Join(",", qxList);

            List<string> dlList = new List<string>();
            var dls = customFilter["FCBDL"] as DynamicObjectCollection;
            foreach (var dl in dls)
            {
                string id = Convert.ToString(dl["FCBDL_Id"]);
                dlList.Add(id);
            }
            string dlStr = string.Join(",", dlList);

            string filterStr = "";
            if (filter.FilterParameter.FilterString.Length > 0)
            {
                filterStr = " where " + filter.FilterParameter.FilterString + $" and FSALEORGID in ({idstr})";
            }
            else
            {
                filterStr = $" where FSALEORGID in ({idstr})";
            }
            if (filterStr.Length > 0 && khStr.Length > 0)
            {
                filterStr += $" and FCUSTOMERID in ({khStr})";
            }
            if (filterStr.Length > 0 && qxStr.Length > 0)
            {
                //这里是筛选砖型
                //filterStr += $" and FCUSTOMERID in ({khStr})";
            }
            if (filterStr.Length > 0 && dlList.Count > 0)
            {
                // 转义单引号并用单引号包裹每个值
                var escapedValues = dlList.Select(s => $"'{s.Replace("'", "''")}'");
                string cutids = string.Join(",", escapedValues);
                //filStr += $" AND T4.FNAME IN ({cutids})";
                filterStr += $" and F_POSW_CBDL in ({cutids})";
            }

            string dataSql = "";
            string endSql = "";
            string sql = "";
            if (zdqys.Count > 0)
            {
                dataSql = GetDataSqlInZd(strDate, endDate);
                endSql = GetSqlInZd(seqFld, tableName, filterStr);
                sql = createSql + dataSql + endSql;
            }
            else
            {
                dataSql = GetDataSql(strDate, endDate);
                endSql = GetSql(seqFld, tableName, filterStr);
                sql = createSql + dataSql + endSql;
            }


            DBUtils.ExecuteDynamicObject(this.Context, sql);
        }

        private string GetSqlInZd(string seqFld, string tableName, string filterStr)
        {
            string endSql = $" select * ,{seqFld} into {tableName} from #TEMP {filterStr} ";
            //string endSql = $" select *  into {tableName} from #TEMP {filterStr} ";
            return endSql;
        }

        private string GetDataSqlInZd(DateTime strDate, DateTime endDate)
        {
            string dataSql = $" INSERT INTO #TEMP EXEC P_POSW_GetYHZDCLCBHZB_ZD '{strDate}','{endDate}' ";
            return dataSql;
        }

        private string GetSql(string seqFld, string tableName, string filterStr)
        {
            string endSql = $" select * ,{seqFld} into {tableName} from #TEMP {filterStr} ";
            //string endSql = $" select *  into {tableName} from #TEMP {filterStr} ";
            return endSql;
        }

        private string GetDataSql(DateTime strDate, DateTime endDate)
        {
            string dataSql = $" INSERT INTO #TEMP EXEC P_POSW_GetYHZDCLCBHZB '{strDate}','{endDate}' ";
            return dataSql;
        }

        private string GetCreateSql()
        {
            string createSql = @"/*dialect*/ CREATE TABLE #TEMP (
            FID INT NULL,
            FKH VARCHAR(255) NULL,
            FDL VARCHAR(255) NULL,
            FEL VARCHAR(255) NULL,
            FLB VARCHAR(255) NULL,
            FREALQTY DECIMAL(38,10) NOT NULL,
            FZSHYL DECIMAL(38,10) NOT NULL,
            FCL DECIMAL(38,10) NOT NULL,
            FZSHCL DECIMAL(38,10) NOT NULL,
            FXHTXT VARCHAR(255) NULL,
            FXH2 VARCHAR(255) NULL,
            FCLCB DECIMAL(38,10) NOT NULL,
            FRGCB DECIMAL(38,10) NOT NULL,
            FYSCB DECIMAL(38,10) NOT NULL,
            FQTCB DECIMAL(38,10) NOT NULL,
            FDWRGCB DECIMAL(38,10) NOT NULL,
            FZSQSYL DECIMAL(38,10) NOT NULL,
            FZSHSYL DECIMAL(38,10) NOT NULL,
            FSNDWCLCB DECIMAL(38,10) NOT NULL,
            FBQDWCLCB DECIMAL(38,10) NOT NULL,
            FBQDWCLCBHZ DECIMAL(38,10) NOT NULL,
            FDWCLCBBD DECIMAL(38,10) NOT NULL,
            FSNDWCB DECIMAL(38,10) NOT NULL,
            FBQDWCB DECIMAL(38,10) NOT NULL,
            FBQDWCBHZ DECIMAL(38,10) NOT NULL,
            FDWCBBDBL DECIMAL(38,10) NOT NULL,
            FSALEORGID INT NULL,
            FZDQY VARCHAR(255) NULL,
            FZDQYNAME VARCHAR(255) NULL,
            FCUSTOMERID INT NULL,
            F_POSW_CBDL VARCHAR(255) NULL
            ) ";
            return createSql;//F_POSW_CBDL
        }
    }
}
